<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>办事处管理</title>
</head>
<body>
	<div class="layui-form" lay-filter="region-search">
		<div class="layui-input-inline">
			<input type="text" name="regionName" placeholder="请输入办事处\部门名称"
				autocomplete="off" class="layui-input">
		</div>
		<button class="layui-btn layuiadmin-btn-list" lay-submit
			lay-filter="regionName">
			<i class="layui-icon layui-icon-search"></i>
		</button>
		<div class="layui-input-inline" style="margin-left: 30px">
			<button class="layui-btn" id="addregion">新增</button>
		</div>
	</div>
	<table class="layui-hide" id="regionTable" lay-filter="regionTable"></table>

	<script type="text/javascript">
		var table = layui.table;
		var form = layui.form;
		var searchForm = form.render(null,'region-search');
	
		searchForm.on('submit(regionName)', function(data) {
			tableIns.reload({
				where : {
					'regionName' : data.field.regionName
				},
				page:{
					curr:1
				}
				
			});
			return false;
		});
		
		var addForm = form.render(null,'region-add-win');
		
		addForm.verify({
			regionName : function(value, item) { //value：表单的值、item：表单的DOM对象
				if (value.trim() == '') {
					return '请输入办事处名称';
				}
			},
			manager: function(value,item) { //value：表单的值、item：表单的DOM对象
				if (value.trim() == '') {
					return '请输入服务经理';
				}
			}
		});
		
		var isUpdate = false;
		
		addForm.on('submit(addregion)',function(data){
			var url = window.basePath+  '/sys/saveRegion';
			if(isUpdate){
				url = window.basePath+ '/sys/updateRegion';
			}
		
			$.ajax({
				  url:url,
					headers: {"token" : window.token},
					method: "post",
				  dataType:'json',
				  data:data.field,
				  success:function(data){
					  if(data){
						  layer.msg(data.msg);
					  }
					  layer.close(layerIndex);
					  $('#region-add-win').trigger('close');
					  tableIns.reload();
				  }
			  });
			return false;
		});
			
		$('#region-add-win').on('close',function(){
			//右上角关闭回调
	    	$('#region-add-win').hide();
		});
		
		var layerIndex;
		$('#addregion').on('click', function(data) {
			isUpdate = false;
			$('#region-add-win')[0].reset();
			layerIndex = layer.open({
				type : 1,
				shade: 0,
				title : '新增办事处/部门',
				area : [ '390px', '260px' ],
				btnAlign : 'c',
				content:$('#region-add-win'),
				cancel: function(){ 
			      //右上角关闭回调
			    	$('#region-add-win').hide();
			   }

			});
		});
		
		var tableIns = table.render({
			elem : '#regionTable',
			url : window.basePath+ '/sys/findRegionByPage',
			headers: {"token" : window.token},
			method: "post",
			height : 'full-150',
			cols : [ [{
				field : 'regionName',
				width : 200,
				align : 'center',
				title : '办事处/部门'
			}, {
				field : 'manager',
				width : 200,
				align : 'center',
				title : '部门主管'
			}, {
				field : 'workNo',
				width : 200,
				align : 'center',
				title : '部门主管工号'
			},{
				fixed : 'right',
				width : 200,
				align : 'center',
				toolbar : '#RbarDemo'
			} ] ],
			text : {
				none : '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
			},
			page : true
		});

		table.on('tool(regionTable)', function(obj) {
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			if (layEvent === 'del') {
				layer.confirm('您确定要删除吗？', function(index) {
					obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
					layer.close(index);
					//向服务端发送删除指令
					$.ajax({
						url : window.basePath+ '/sys/deleteRegion',
						headers: {"token" : window.token},
						method: "post",
						dataType : 'json',
						data : {
							Id : data.id
						},
						success : function(data) {
							if (data) {
								layer.msg(data.msg);
							}
							tableIns.reload();
						}
					});
				});
			}
			if (layEvent === 'update') {
				isUpdate = true;
				$.ajax({
					url :window.basePath+  '/sys/findRegionManager',
					headers: {"token" : window.token},
					method: "post",
					dataType : 'json',
					data : {
						Id : data.id
					},
					success : function(data) {
						if (data.success === true) {
							addForm.val('region-add-win',data.obj);
							layerIndex = layer.open({
								type : 1,
								shade: 0,
								title : '修改办事处/部门',
								area : [ '390px', '260px' ],
								btnAlign : 'c',
								content:$('#region-add-win'),
								cancel: function(){ 
							      //右上角关闭回调
							    	$('#region-add-win').hide();
							   }

							});
						}else{
							layer.msg(data.msg);
						}
					}
				});
			};
			
		});
		
		
		$('#selectManager').on('click',function(){
			showDialog({
				title:'选择部门主管',			
				template:window.basePath + '/admin/views/sys/users.html',
				showbtns:true,
				width:750,
				height:550,
				yes:function(index){
					var checkTempData = window.userTable.checkStatus('userTable');
					var data = checkTempData.data;
					if(data && data.length >0){
						if(data.length>1){
							layer.msg('只能选择一条数据');
						}else{
							$('#region-add-win').find('input[name="manager"]').val(data[0].EmpName);
							$('#region-add-win').find('input[name="workNo"]').val(data[0].EmpCode);
							$('#region-add-win').find('input[name="orgName"]').val(data[0].OrgName);
							layer.close(index);
						}
					}
				}
			});
		});
	</script>
	<script type="text/html" id="RbarDemo">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<form   id="region-add-win" class="winhidden layui-form" lay-filter="region-add-win" style="padding-top:30px;">
<div class="layui-form-item">
	<div class="layui-inline">
		<label class="layui-form-label">办事处/部门:</label>
		<div class="layui-input-inline">
			<input type="hidden" name="id">
			<input type="text" name="regionName" placeholder="请输入办事处名称"
				autocomplete="off" class="layui-input" lay-verify="regionName">
		</div>
	</div>
</div>
<div class="layui-form-item">
	<div class="layui-inline">
		<label class="layui-form-label">部门主管:</label>
		<div class="layui-input-inline">
			<input type="text" name="manager" placeholder="请输入服务经理"
				autocomplete="off" class="layui-input" lay-verify="manager">
				<input type="hidden" name="workNo"/>
				<input type="hidden" name="orgName"/>
		</div>
		<div class="layui-form-mid layui-word-aux">
	   		<i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="selectManager"></i>
	   </div>
	</div>
</div>

<div class="layui-form-item">
	<div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="addregion">提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form >

</body>
</html>

